package com.qidian.study.servlets;

import com.qidian.study.service.UserBaseService;
import com.qidian.study.service.impl.UserBaseServiceImpl;
import com.qidian.study.util.JsonTools;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 戴着假发的程序员
 * @company 起点编程
 */
@WebServlet("/userlogin")
public class UserLoginServlet extends BaseServlet {
    private UserBaseService userBaseService = new UserBaseServiceImpl();
    public void doLogin(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
        String username = request.getParameter("username");
        String userpass = request.getParameter("userpass");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(userBaseService.login(username,userpass)));
    }

    public void exit(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        String token = request.getParameter("token");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(userBaseService.exit(token)));
    }

    public void checkLogin(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        String token = request.getParameter("token");
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JsonTools.object2json(userBaseService.checkLogin(token)));
    }
}
